package xin.ssm.jxc.controller;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import xin.ssm.jxc.model.GoodsSales;
import xin.ssm.jxc.services.ISaleService;
import xin.ssm.jxc.util.Result;




@Controller
@RequestMapping("/sale")
public class SaleController {
	@Resource
	private ISaleService saleService;
	// 查询订单
			@RequestMapping("/findsaledynamic")
			@ResponseBody
			public Result findSaleDynamic(
					@RequestParam(value = "userloginname") String userloginname,
					@RequestParam(value = "employeecode") String employeecode,
					@RequestParam(value = "goodsid") String goodsid,
					@RequestParam(value = "depotcode") String depotcode,
					@RequestParam(value = "salesstatus") String salesstatus) {
				
				if (userloginname.equals("") || userloginname.isEmpty()) {
					userloginname = null;
				}
				if (employeecode.equals("") || employeecode.isEmpty()) {
					employeecode = null;
				}
				if (goodsid.equals("") || employeecode.isEmpty()) {
					goodsid = "0";
				}
				if (depotcode.equals("") || depotcode.isEmpty()) {
					depotcode = null;
				}
				if (salesstatus.equals("") || salesstatus.isEmpty()) {
					salesstatus = null;
				}
				GoodsSales gs = new GoodsSales();
				gs.setUserloginname(userloginname);
				gs.setEmployeecode(employeecode);
				gs.setGoodsid(Integer.parseInt(goodsid));
				gs.setDepotcode(depotcode);
				gs.setSalesstatus(salesstatus);
				
				List<GoodsSales> gsList = saleService.findGoodsSaleDynamic(gs);
				Result result = new Result();
				result.setCode(0);
				result.setMsg("成功");
				result.setCount(gsList.size());
				result.setData(gsList);
				return result;
			}
//修改			
			@RequestMapping("/updatesale")
			@ResponseBody
			public String updateSale(
					@RequestParam(value = "salesid") Integer salesid,
					@RequestParam(value = "userloginname") String userloginname,
					@RequestParam(value = "employeecode") String employeecode,
					@RequestParam(value = "depotcode") String depotcode,
					@RequestParam(value = "goodsid") Integer goodsid) {// 类别id
				GoodsSales gs = new GoodsSales();
				gs.setSalesid(salesid);
				gs.setUserloginname(userloginname);
				gs.setEmployeecode(employeecode);
				gs.setDepotcode(depotcode);
				gs.setGoodsid(goodsid);
				int ret = saleService.updateSale(gs);
				if (ret > 0) {
					return "1";
				} else {
					return "0";
				}
			}
			
			@RequestMapping("/insertsale")
			@ResponseBody
			public String insertSale(
					@RequestParam(value = "salesnum") Integer salesnum,
					@RequestParam(value = "userloginname") String userloginname,
					@RequestParam(value = "employeecode") String employeecode,
					@RequestParam(value = "depotcode") String depotcode,
					@RequestParam(value = "goodsid") Integer goodsid) {
				GoodsSales gs = new GoodsSales();
				gs.setSalesnum(salesnum);
				gs.setUserloginname(userloginname);
				gs.setEmployeecode(employeecode);
				gs.setDepotcode(depotcode);
				gs.setGoodsid(goodsid);
				int ret = saleService.addSale(gs);
				if (ret > 0) {
					return "1";
				} else {
					return "0";
				}
			}
			
			@RequestMapping("/deletesale")
			@ResponseBody
			public String deleteSale(@RequestParam(value = "salesid") Integer salesid) {
				int ret = saleService.deleteSale(salesid);
				if (ret > 0) {
					return "1";
				} else {
					return "0";
				}
			}

}
